package com.cuit.mimall.controller.backend;

import com.cuit.mimall.common.R;
import com.cuit.mimall.entity.User;
import com.cuit.mimall.mapper.UserMapper;
import com.cuit.mimall.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author : TeaWu
 * @folder  后台/用户
 */
@Slf4j
@RestController("backendUserController")
@RequestMapping("/api/v1/backend/user")
public class UserController {

    private final IUserService userService;
    //12


    //比茗的测试
    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    /**
     * 分页获取用户列表
     *
     * @param pageNumber  页码 默认1
     * @param pageSize 页大小 默认10
     */
    @GetMapping("/getUsers")
    @PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
    public R<?> getUsers(@RequestParam(defaultValue = "1") Integer pageNumber,
                         @RequestParam(defaultValue = "10") Integer pageSize) {
        return userService.getUsers(pageNumber, pageSize);
    }

    /**
     * 按状态分页获取用户列表
     *
     * @param status   用户状态 0-启用 1-禁用
     * @param pageNum  页码 默认1
     * @param pageSize 页大小 默认10
     */
    @GetMapping("/getUsersByStatus")
    @PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
    public R<?> getUsersByStatus(@RequestParam Integer status,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("status:{}, pageNum:{}, pageSize:{}", status, pageNum, pageSize);
        return userService.getUsersByStatus(status, pageNum, pageSize);
    }

    @PostMapping("/addUser")
    @PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
    public R<?> addUser(@RequestBody User user) {
        log.info("addUser...");
        return userService.addUser(user);
    }

    @PutMapping("/updateUser")
    @PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
    public R<?> UpdateUser(@RequestBody User user) {
        log.info("update user ...");
        return userService.updateUser(user);
    }
}
